package com.sxt.practiceapplication.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.activities.GuideActivity;
import com.sxt.practiceapplication.databinding.InfoItemBinding;
import com.sxt.practiceapplication.utils.DisplayUtil;

public class InfoItemView extends RelativeLayout {

    /**
     * 左边标题
     */
    private final TextView infoTitle;

    /**
     * 右边值
     */
    private final TextView infoValue;

    /**
     * 右边的箭头图标
     */
    private final TextView rightPoint;

    public InfoItemView(Context context) {
        this(context, null);
    }

    public InfoItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InfoItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public InfoItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        InfoItemBinding inflate = InfoItemBinding.inflate(LayoutInflater.from(context), this, true);
        infoTitle = inflate.infoTitle;
        infoValue = inflate.infoValue;
        rightPoint = inflate.rightPoint;

        initStyle(context, attrs);

    }

    private void initStyle(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InfoItemView);
        infoTitle.setText(typedArray.getString(R.styleable.InfoItemView_info_title));
        infoValue.setText(typedArray.getString(R.styleable.InfoItemView_info_value));
        if (!typedArray.getBoolean(R.styleable.InfoItemView_show_right_icon, true)) {
            rightPoint.setVisibility(INVISIBLE);
        }
        float screenDensity = DisplayUtil.getScreenDensity(context);
        float titleTextSize = typedArray.getDimension(R.styleable.InfoItemView_title_text_size,
                DisplayUtil.sp2px(20, screenDensity));
        infoTitle.setTextSize(DisplayUtil.px2sp(titleTextSize, screenDensity));
        float valueTextSize = typedArray.getDimension(R.styleable.InfoItemView_value_text_size,
                DisplayUtil.sp2px(18, screenDensity));
        infoValue.setTextSize(DisplayUtil.px2sp(valueTextSize, screenDensity));
        typedArray.recycle();
    }

    public void setOnInfoItemViewClickListener(OnClickListener listener) {
        this.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(InfoItemView.this);
            }
        });

    }

    public interface OnClickListener {
        void onClick(InfoItemView view);
    }

    public TextView getInfoTitle() {
        return infoTitle;
    }

    public TextView getInfoValue() {
        return infoValue;
    }
}
